package com.zz.springcloud.controller;

import com.zz.springcloud.entity.Member;
import com.zz.springcloud.service.MemberService;
import com.zz.springcloud.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/member")
public class MemberController {
    @Resource
    private MemberService memberService;
    @Value("${server.port}")
    private Integer port;

    @PostMapping(value = "/save")
    public Result save(@RequestBody Member member) {
        int result = memberService.save(member);
        log.info("reset= " + result);
        if (result > 0) { //成功
            return Result.success("添加用户成功".concat(String.valueOf(port)), result);
        } else {
            return Result.error("401", "添加用户失败");
        }
    }

    @GetMapping(value = "/get/{id}")
    public Result getMemberById(@PathVariable("id") Long id, HttpServletRequest request) {
        Member member = memberService.queryMemberById(id);
        log.info("查询结果= " + member);
//        log.info("颜色" + request.getParameter("color").toString());
//        log.info("年龄" + request.getParameter("age").toString());
        if (member != null) {
            return Result.success("查询成功".concat(String.valueOf(port)), member);
        }else{
            return Result.error("402", "ID= " + id + " 不存在");
        }
    }
}
